package com.spar.openprojects.client.composite;

import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.DefaultFormat;
import com.spar.openprojects.client.RemoteDataService;
import com.spar.openprojects.client.RemoteDataServiceAsync;
import com.spar.openprojects.shared.Member;
import com.spar.openprojects.shared.OrderItem;

public class OrderItemComposite extends Composite {
	private RemoteDataServiceAsync asyncService = GWT
			.create(RemoteDataService.class);
	private OrderItem orderItem = new OrderItem();
	private Label labelName;
	private Member user = new Member();
	private TextBox textBoxAmount;
	private TextBox textBoxNumber;
	private TextBox textBoxLink;
	private DateBox dateBoxDate;
	private DateBox dateBoxReleaseDate;
	private Button buttonDelete = new Button("Delete");
	private HorizontalPanel horizontalPanel = new HorizontalPanel();

	public OrderItemComposite(final OrderItem orderItem, String label,
			final Member user) {

		this.orderItem = orderItem;
		this.user = user;
		horizontalPanel = new HorizontalPanel();
		horizontalPanel.setSpacing(10);

		labelName = new Label(label);
		labelName.setWidth("50px");
		horizontalPanel.add(labelName);

		textBoxAmount = new TextBox();
		textBoxAmount.setVisibleLength(9);
		horizontalPanel.add(textBoxAmount);
		textBoxAmount.setWidth("100px");

		textBoxNumber = new TextBox();
		textBoxNumber.setVisibleLength(12);
		horizontalPanel.add(textBoxNumber);
		textBoxNumber.setWidth("150px");

		textBoxLink = new TextBox();
		horizontalPanel.add(textBoxLink);
		textBoxLink.setWidth("200px");

		dateBoxDate = new DateBox();
		dateBoxDate.setFormat(new DefaultFormat(DateTimeFormat
				.getFormat("y MM d")));
		horizontalPanel.add(dateBoxDate);
		dateBoxDate.setWidth("100px");

		dateBoxReleaseDate = new DateBox();
		dateBoxReleaseDate.setFormat(new DefaultFormat(DateTimeFormat
				.getFormat("y MM d")));
		dateBoxReleaseDate.setWidth("100px");

		if (label.equals("Bill")) {
			horizontalPanel.add(dateBoxReleaseDate);
			horizontalPanel.add(buttonDelete);
		}

		textBoxAmount.setText(orderItem.getAmount().toString());
		textBoxNumber.setText(orderItem.getNumber());
		textBoxLink.setText(orderItem.getLink());
		dateBoxDate.setValue(orderItem.getDate());
		dateBoxReleaseDate.setValue(orderItem.getReleaseDate());

		initWidget(horizontalPanel);

		buttonDelete.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				deleteOrderItem(user);
			}
		});

	}

	private void deleteOrderItem(Member user) {
		if (orderItem.getId() == null || orderItem.getId() == 0) {
			removeOrderItemComposite();
		} else {
			asyncService.deleteOrderItem(orderItem, user,
					new AsyncCallback<Void>() {

						@Override
						public void onSuccess(Void result) {
							RootPanel.get("infoContainer").clear();
							RootPanel.get("infoContainer").add(
									new Label("Bill deleted(id:"
											+ orderItem.getId()
											+ ") for orderid "
											+ orderItem.getOrderId()));
							removeOrderItemComposite();
						}

						@Override
						public void onFailure(Throwable caught) {
							RootPanel.get("infoContainer").clear();
							RootPanel.get("infoContainer").add(
									new Label(caught.getMessage()));
						}
					});
		}
	}

	private void removeOrderItemComposite() {
		Iterator<Widget> iterator = RootPanel.get("openprojectsContainer")
				.iterator();
		Widget widget;
		while (iterator.hasNext()) {
			widget = iterator.next();
			if (widget instanceof OrderComposite) {
				((OrderComposite) widget).getOrder().getBills()
						.remove(orderItem);
				((OrderComposite) widget).updateBills();
			}
		}
	}

	public OrderItem getOrderItem() {
		orderItem.setAmount(Double.valueOf(textBoxAmount.getValue()));
		orderItem.setNumber(textBoxNumber.getText());
		orderItem.setLink(textBoxLink.getText());
		orderItem.setDate(dateBoxDate.getValue());
		orderItem.setReleaseDate(dateBoxReleaseDate.getValue());

		return orderItem;
	}
}
